# -*- coding: utf-8 -*-
"""
Created on Thu Oct 27 16:53:47 2016

@author: jocke_000
"""

import cPickle

class ForExample:
    def __init__(self):
        self.documents = ['I','You','He/She']
        self.count = 2.1112
        self.dictA = {1:'One', 2:'Two', 3:'Three', 4:'Four'}
        self.dictB = {11:'Eleven', 12:'Twelve', 13:'Thirteen', 14:'Forteen'}
        self.file = open('testpickle', 'w')
        
    def __getstate__(self):
#        def normalize(x):
#            if type(x) == types.FileType:
#                return 1, (x.name, x.mode, x.tell())
#            return 0, x
#        return [ normalize(x) for x in self.__dict__ ]
        f = self.file
        return (self.documents, self.count, self.dictA, self.dictB, f.name, f.mode, f.tell())
        
    def __setstate__(self, state):
#        def reconstruct(x):
#            if x[0] == 0:
#                return x[1]
#            name, mode, offs = x[1]
#            openfile = open(name, mode)
#            openfile.seek(offs)
#            return openfile
#        for x in d:
#            self.__dict__[x] = reconstruct(x)
        #self.__dict__ = set([reconstruct(x) for x in d])
        self.documents, self.count, self.dictA, self.dictB, fname, fmode, ftell = state
        f = open(fname, fmode)
        f.seek(ftell)
        self.file = f
        
one = ForExample()
saved = cPickle.dump(one, open('my.pickle', 'wb'))
two = cPickle.load(open('my.pickle', 'rb'))
#assert anInstance.stuff == reloaded.stuff
